<?php
namespace source\models;

use yii\base\Model;
use yii\web\UploadedFile;

/**
 * 文件上传类
 *
 * 主要用于文件上传（详细说明）
 * @author         万志均 <wzjun@zhutxia.com>
 * @since          1.0
 */
class UploadForm extends Model
{
	/**
	 * @var uploadFiles[]
	 */
	public $uploadFiles;
	public $type;
	
	public function rules()
	{
		return [
				[['uploadFiles'], 'file', 'skipOnEmpty' => false,  'maxFiles' => 4],
		];
	}
	
	public function upload()
	{
		if ($this->validate()) {
			$resArr = array();
			$ymd = date("Ymd");
			$save_path = \Yii::getAlias('@attachmentPath') . '/csv/';
// 			$save_path = '/public/uploads/csv';
			$save_url = 'uploads/csv';
			
			if(! file_exists($save_path))
			{
				mkdir($save_path);
			}
			
			foreach ($this->uploadFiles as $file) {
				
				$file_name = $file->baseName;
				$file_ext = $file->extension;
				// 新文件名
				$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
				
				$file->saveAs($save_path . $new_file_name);
				
				$resArr[] = ['path' => $save_path, 'url' => $save_url, 'name' => $file_name, 'new_name' => $new_file_name, 'ext' => $file_ext,
						'full_name'=>$save_url.$new_file_name,
						'temp_name'=>$file->tempName,'type'=>$file->type,'size'=>$file->size,'message'=>'ok'];
				
			}
			return $resArr;
		} else {
			return false;
		}
	}
	
}